<?php

/**
 * 新闻
 */

namespace app\api\controller\material;

use app\api\controller\Common;

class Material extends Common
{
    protected $data;
    public function initialize()
    {
        parent::initialize();
        $this->data = $this->request->except(['sign', 'accessKey', 'token']);
    }

    //图文素材列表
    public function articleList()
    {
        $type = input('type_id', 0);
        $keyword = input('keyword', '');
        $page = $this->data['page'] ? $this->data['page'] : 1;
        $pageSize = $this->data['limit'] ? $this->data['limit'] : 10;
        $paginate = ['list_rows' => $pageSize, 'page' => $page];

        $where = [];
        $where[] = ['status', '=', 1];
        if ($type) {
            $where[] = ['type', '=', $type];
        }
        if ($keyword) {
            $where[] = ['title', 'like', '%' . $keyword . '%'];
        }
       
        $list = db('material_article')
            ->field('id, title, cover, desc, video, create_time')
            ->where($where)->order('update_time', 'desc')->paginate($paginate)->toArray();
        if($list){
            foreach($list['data'] as $key=>$val){
                $pic = db('material_file')
                    ->field('id, url')
                    ->where('article_id', $val['id'])->select();
                $list['data'][$key]['pic'] = $pic;
            }
        }
        return $this->showSuccess('图文素材', $list);
    }

    //全部素材列表
    public function fileList()
    {

        $keyword = input('keyword', '');
        $pid = input('pid', 0);
        $page = $this->data['page'] ? $this->data['page'] : 1;
        $pageSize = $this->data['limit'] ? $this->data['limit'] : 10;
        $paginate = ['list_rows' => $pageSize, 'page' => $page];

        $where = [];
        $where[] = ['del', '=', 0];
        $where[] = ['status', '=', 1];
        if($pid >= 0){
            $where[] = ['pid', '=', $pid];
        }

        if ($keyword) {
            $where[] = ['title', 'like', '%' . $keyword . '%'];
        }

        $list = db('material')
        ->field('id, type, title, url, create_time')
        ->where($where)->order('update_time', 'desc')->paginate($paginate)->toArray();

        if($pid > 0){
            $title = db('material')->where('id', $pid)->value('title');
        }
        else{
            $title = '全部素材';
        }
        
        return $this->showSuccess('全部素材', $list, ['title'=>$title]);
    }

    
}
